package com.rabbitmq.ChattingRoom.util.common.base;

import com.rabbitmq.ChattingRoom.util.common.enums.ResultEnum;
import lombok.Data;

@Data
public class Result<T> {

    private Integer code;
    private boolean success;
    private String msg;
    private T data;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    private Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        if (code == 200) {
            this.success = true;
        } else {
            this.success = false;
        }
    }

    public static <T> Result<T> success(String msg, T data) {
        return new Result<T>(ResultEnum.SUCCESS.getCode(), msg, data);
    }

    public static <T> Result<T> success(T data) {
        ResultEnum anEnum = ResultEnum.SUCCESS;
        return new Result<T>(anEnum.getCode(), anEnum.getMessage(), data);
    }

    public static <T> Result<T> success(String msg) {
        return new Result<T>(ResultEnum.SUCCESS.getCode(), msg, null);
    }

    public static <T> Result<T> error(String msg, T data) {
        return new Result<T>(ResultEnum.ERROR.getCode(), msg, data);
    }

    public static <T> Result<T> error(T data) {
        ResultEnum anEnum = ResultEnum.ERROR;
        return new Result<T>(anEnum.getCode(), anEnum.getMessage(), data);
    }

    public static Result<String> authorTimeout() {
        return new Result<String>(ResultEnum.AUTHOR_TIMEOUT.getCode(), ResultEnum.AUTHOR_TIMEOUT.getMessage(), null);
    }

    public static <T> Result<T> error(String msg) {
        return new Result<T>(ResultEnum.ERROR.getCode(), msg, null);
    }

    public static <T> Result<T> custom(ResultEnum resultEnum, T data) {
        return new Result<T>(resultEnum.getCode(), resultEnum.getMessage(), data);
    }

}
